import http from '../utils/http';
import { SET_USER_LIST,SET_GROUP_LIST,SET_TAG_LIST,SET_PROFILE,SET_GROUP_DETAIL,REMOVE_GROUPS,SET_TAG_DETAIL,REMOVE_TAGS,REMOVE_USERS } from './types';

export function setProfile(profile){
  return {
    type:SET_PROFILE,
    profile
  }
}

export function setUserList(users) {
  return {
    type: SET_USER_LIST,
    users
  };
}

export function setGroupList(groups) {
  return {
    type: SET_GROUP_LIST,
    groups
  };
}

export function removeGroups(groups){
  return {
    type:REMOVE_GROUPS,
    groups
  }
}

export function removeUsers(users){
  return {
    type:REMOVE_USERS,
    users
  }
}

export function setGroup(group){
  return {
    type:SET_GROUP_DETAIL,
    group
  }
}

export function setTag(tag){
  return {
    type:SET_TAG_DETAIL,
    tag
  }
}

export function removeTags(tags){
  return {
    type:REMOVE_TAGS,
    tags
  }
}

export function setTagList(tags){
    return {
    type: SET_TAG_LIST,
    tags
  };
}

export function addUser(data) {
  return dispatch => {
      return http.post('/api/users',data).then(res => {
              return res ;
            });
      }
}

export function updateUser(data){
  return dispatch => {
      return http.post('/api/users/update',data).then(res => {
              return res ;
            });
      }
}

export function fetchUser(id){
  return dispatch => {
      return http.get('/api/users/'+id).then(res => {
              const profile = res.data.body.profile || [] ;
              dispatch(setProfile(profile));
              return profile ;
          });
    }
}

export function fetchUsers(fields,page,pageSize,filters) {  
    page = typeof page !== 'undefined' ? page : 1;
    pageSize = typeof pageSize !== 'undefined' ? pageSize : 10;
    return dispatch => {
      return http.get('/api/users', {
                params: {
                  fields,
                  page:page,
                  pageSize:pageSize,
                  ...filters
                }
            }).then(res => {
              const users = res.data.body.users || [] ;
              dispatch(setUserList(users));
              return users ;
          });
    }
}

export function fetchGroups(page,pageSize) {  
  page = typeof page !== 'undefined' ? page : 1;
  pageSize = typeof pageSize !== 'undefined' ? pageSize : 10;
  return dispatch => {
    return http.get('/api/groups', {
                params: {
                  page:page,
                  pageSize:pageSize,
                }
            }).then(res => {
          const groups = res.data.body.groups || [];
          dispatch(setGroupList(groups));
          return groups;
        });
  }
}

export function fetchGroup(id,with_users){
  return dispatch => {
    return http.get('/api/groups/'+id, {
                params: {
                  with_users:with_users
                }
            }).then(res => {
          const group = res.data.body.group || [];
          dispatch(setGroup(group));
          return group;
        });
  }
}

export function fetchTags(page,pageSize) {  
  page = typeof page !== 'undefined' ? page : 1;
  pageSize = typeof pageSize !== 'undefined' ? pageSize : 10;
  return dispatch => {
    return http.get('/api/tags', {
              params: {
                page:page,
                pageSize:pageSize,
              }
          }).then(res => {
            const tags = res.data.body.tags || [];
          dispatch(setTagList(tags));
          return tags;
        });
  }
}
export function fetchTag(id,with_users){
  return dispatch => {
    return http.get('/api/tags/'+id, {
                params: {
                  with_users:with_users
                }
            }).then(res => {
          const tag = res.data.body.tag || [];
          dispatch(setTag(tag));
          return tag;
        });
  }
}

export function deleteUser(ids) {
  return dispatch => {
      return http.post('/api/users/delete', {ids}).then(res => {
          dispatch(removeUsers(ids));
            return res ; 
        });
    }
}

export function deleteGroups(ids){
  return dispatch => {
      return http.post('/api/groups/delete', {ids}).then(res => {
           dispatch(removeGroups(ids));
        });
    }
}

export function deleteTags(ids){
  return dispatch => {
      return http.post('/api/tags/delete', {ids}).then(res => {
           dispatch(removeTags(ids));
        });
    }
}

export function saveGroup(data){
  return dispatch => {
      return http.post('/api/groups',data).then(res => {
              return res ;
            });
      }
}

export function saveTag(data){
  return dispatch => {
      return http.post('/api/tags',data).then(res => {
              return res ;
            });
      }
}